package tree;

import java.util.ArrayList;
import java.util.List;

public class TreeNode<T> {
    private String id;
    private String wellcode;
    private String parentId;
    private String text;
    private String name;
    private Boolean leaf = true;
    private Boolean expanded = false;
    private Boolean open = false;
    
    public String getWellcode() {
		return wellcode;
	}

	public void setWellcode(String wellcode) {
		this.wellcode = wellcode;
	}

	public void setOpen(Boolean open) {
		this.open = open;
	}
	public Boolean getOpen() {
		return open;
	}
	private T nodeData;

    /**
     * �Ƿ�Ϊ���ڵ㣬���ֶκ�leaf�ظ�����Ҫ��Ϊ�˼���Ext��zTree
     */
    private Boolean isParent = false;

    /**
     * �ӽڵ㣬���û���ӽڵ㣬���б?��Ϊ0
     */
    private List<TreeNode<T>> children = new ArrayList<TreeNode<T>>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.name = text;
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        this.text = name;
    }

    public Boolean getExpanded() {
        return expanded;
    }

    public void setExpanded(Boolean expanded) {
        this.expanded = expanded;
    }

    public List<TreeNode<T>> getChildren() {
        return children;
    }

    public void setLeaf(Boolean leaf) {
        this.leaf = leaf;
        this.isParent = !leaf;
    }

    public Boolean getLeaf() {
        return this.leaf;
    }

    public Boolean getIsParent() {
        return isParent;
    }

    public void setIsParent(Boolean isParent) {
        this.isParent = isParent;
        this.leaf = !isParent;
    }

    public T getNodeData() {
        return nodeData;
    }

    public void setNodeData(T nodeData) {
        this.nodeData = nodeData;
    }

    /**
     * �����ڵ��б?���������󷵻����ĸ�ڵ㣬�������б��ж����ڵ㣬�ᶯ̬����һ����ڵ㡣
     * @param nodes ���ڵ��б�
     * @return ��ڵ�
     */
    public static <T> TreeNode<T> buildTree(List<TreeNode<T>> nodes){
        if(nodes == null || nodes.size() == 0){
            return null;
        }

        if(nodes.size() == 1){
            return nodes.get(0);
        }

        //�������nodes����Ķ������ڵ�
        //Ҳ���ǰ�û�и��ڵ�Ľڵ㶼�ŵ�tops����ȥ
        List<TreeNode<T>> tops = new ArrayList<TreeNode<T>>();

        boolean hasParent = false;
        //��һ�α����ȡһ���ڵ���Ϊ�ӽڵ�
        for(TreeNode<T> child : nodes){
            hasParent = false;

            //��ǰ�ڵ�child�ĸ��ڵ�id
            String pid = child.getParentId();

            //���pid�����ڻ�Ϊ��
            //��ǰ�ڵ�Ϊ�����ڵ�
            if(pid == null || pid.equals("")){
                //�ѵ�ǰ�ڵ���ӵ�tops����Ϊ�����ڵ�
                tops.add(child);
                //���ǰ�ڵ㣬������һ��
                continue;
            }

            //����nodes�ϵ����нڵ㣬�ж��Ƿ���child�ĸ��ڵ�
            for(TreeNode<T> parent : nodes){
                String id = parent.getId();

                //���parent�ڵ��id����child�ڵ��pid����parent�ڵ���child�ڵ�ĸ��ڵ�
                if(id != null && id.equals(pid)){

                    //��child�ӵ�parent��
                    parent.getChildren().add(child);
                    parent.setLeaf(false);

                    //child�ڵ��и��ڵ�
                    hasParent = true;

                    continue;
                }
            }

            //���child�ڵ�û�и��ڵ㣬��child�Ƕ����ڵ�
            //��child��ӵ�tops��
            if(!hasParent){
                tops.add(child);
            }
        }

        TreeNode<T> root;
        if(tops.size() == 1){
            //����ڵ�ֻ��һ�����ö����ڵ��Ǹ�ڵ�
            root = tops.get(0);
//            System.out.println("1:"+root);
            
        }else{
            root = new TreeNode<T>();
            root.setLeaf(false);
            root.setId("-1");
            root.setName("油气井");
            root.setParentId("");
            root.setWellcode("-1");
            root.setOpen(true);

            root.getChildren().addAll(tops);
//            System.out.println("2:"+tops);
        }
//        root.setExpanded(true);
//        root.setOpen(true);
//        System.out.println("3:"+root);
        
        return root;
    }
}
